package com.app.oasystem.utils;

import com.app.oasystem.dto.MailDto;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

/**
 * @author zbbstart
 * @date 2021/3/2 - 19:38
 */
public class MailsUtils {
    private MailsUtils() {

    }

    /**
     * 只包含简单文本
     */
    public static MimeMessage createSimpleMail(Session session, InternetAddress from, InternetAddress[] to, String title, String content) throws Exception {
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 指明邮件的发件人
        message.setFrom(from);
        // 指明邮件的收件人，如果发件人和收件人是一样的，那就是自己给自己发
        // 这个地方第二个参数可以是一个数组，表示发给多个人
        message.setRecipients(Message.RecipientType.TO, to);
        // 邮件的标题
        message.setSubject(title);
        // 邮件的文本内容
        message.setContent(content, "text/html;charset=UTF-8");
        // 返回创建好的邮件对象
        return message;
    }

    private static InternetAddress getAddress(String address) {

        try {
            return new InternetAddress(address);
        } catch (AddressException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static InternetAddress[] getAddresses(String adderesses) {
        //多个邮箱之间用逗号隔开
        String[] addressArr = adderesses.split(",");
        InternetAddress[] internetAddresses = new InternetAddress[addressArr.length];
        for (int i = 0; i < addressArr.length; i++) {
            internetAddresses[i] = getAddress(addressArr[i]);
        }
        return internetAddresses;
    }

    public static void sendMail(MailDto mailDto) {
        String fromMail = mailDto.getFromMail();
        Properties prop = new Properties();
        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("mail.smtp.socketFactory.port", "465");
        if (fromMail.contains("qq.com")) {
            prop.setProperty("mail.host", "smtp.qq.com");
        }
        if (fromMail.contains("163.com")) {
            prop.setProperty("mail.host", "smtp.163.com");
        }
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.auth", "true");
        // 创建session
        Session session = Session.getInstance(prop, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //这里是发送方的信息
                return new PasswordAuthentication(mailDto.getUserName(), mailDto.getPassword());
            }
        });
        // 1. 创建简单类型邮件
        Message message = null;
        try {

            message = createSimpleMail(session, getAddress(mailDto.getFromMail()), getAddresses(mailDto.getToMail()), mailDto.getTitle(), mailDto.getMailContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 发送邮件
        try {
            assert message != null;
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
